/*
 * DS1307.h
 *
 *  Created on: Jan 19, 2012
 *      Author: Administrator
 */

#ifndef DS1306_H_
#define DS1306_H_


#include "SPI.h"


	//Read Seconds register
	#define SEC_R 				0x00
	//Read Minutes register
	#define MIN_R				0x01
	//Read Hours register
	#define HOUR_R				0x02
	//Read Days register
	#define DAY_R				0x03
	//Read Date register
	#define	DATE_R				0x04
	//Read Month register
	#define MONTH_R				0x05
	//Read Year register
	#define YEAR_R				0x06

	//Read Seconds Alarm 0 register
	#define SEC_A0_R 			0x07
	//Read Minutes Alarm 0 register
	#define MIN_A0_R			0x08
	//Read Hours Alarm 0 register
	#define HOUR_A0_R			0x09
	//Read Days Alarm 0 register
	#define DAY_A0_R			0x0A

	//Read Seconds Alarm 1 register
	#define SEC_A1_R			0x0B
	//Read Minutes Alarm 1 register
	#define MIN_A1_R			0x0C
	//Read Hours Alarm 1 register
	#define HOUR_A1_R			0x0D
	//Read Days Alarm 1 register
	#define DAY_A1_R			0x0E

	//Read Control register
	#define CONTROL_R 			0x0F
	//Read Status register
	#define STATUS_R			0x10
	//Read Seconds Trickle Charger register
	#define TRICKLE_CHARGER_R 	0x11

	//Read Low RAM address
	#define RAM_LOW_R			0x20
	//Read High RAM address
	#define	RAM_HIGH_R			0x7F

	//Write Seconds Register
	#define SEC_W 				0x80
	//Write Minutes Register
	#define MIN_W				0x81
	//Write Hours Register
	#define HOUR_W				0x82
	//Write Days Register
	#define DAY_W				0x83
	//Write Date Register
	#define	DATE_W				0x84
	//Write Month Register
	#define MONTH_W				0x85
	//Write Year Register
	#define YEAR_W				0x86

	//Write Seconds Alarm 0 Register
	#define SEC_A0_W			0x87
	//Write Minutes Alarm 0 Register
	#define MIN_A0_W			0x88
	//Write Hours Alarm 0 Register
	#define HOUR_A0_W			0x89
	//Write Days Alarm 0 Register
	#define DAY_A0_W			0x8A

	//Write Seconds Alarm 1 Register
	#define SEC_A1_W 			0x8B
	//Write Minutes Alarm 1 Register
	#define MIN_A1_W			0x8C
	//Write Hours Alarm 0 Register
	#define HOUR_A1_W			0x8D
	//Write Days Alarm 0 Register
	#define DAY_A1_W			0x8E

	//Write Control Register
	#define CONTROL_W 			0x8F
	//Write Status Register
	#define STATUS_W			0x90
	//Write Trickle Charger Register
	#define TRICKLE_CHARGER_W 	0x91

	//Write Low RAM address
	#define RAM_LOW_W			0xA0
	//Write high RAM address
	#define	RAM_HIGH_W			0xFF

	//10's second Mask (0-6)
	#define SEC10_MASK			0x70
	//1's second mask (0-9)
	#define SEC_MASK			0x0F
	//10's minute Mask
	#define MIN10_MASK			0x70
	//1's minute mask
	#define MIN_MASK			0x0F
	//1's hour mask (0-9)
	#define HOURS_MASK			0x0F
	//10 hour mask (0-1)
	#define	HOURS10_MASK		0x10
	//AM/PM mask
	#define AM_PM_MASK			0x20
	#define AM_PM_bp			5
	//12 or 24 hour mask
	#define TIME_12_24_MASK		0x40
	//Day of week mask (0-6)
	#define	DAY_MASK			0x07
	//10's Date (day of month) mask (0-3)
	#define	DATE10_MASK			0x30
	//1's date (day of month mask) (0-9)
	#define DATE_MASK			0x0F
	//Write protect mask (clear on start up)
	#define WRITE_PROTECT_bm	0x40
	//Write protect bit position (clear on start up)
	#define WRITE_PROTECT_bp	6
	//1hz output enable mask
	#define EN_1HZ_OUTPUT_bm	0x04
	//1hz output enable bit position
	#define EN_1HZ_OUPUT_bp		2
	//Alarm Interrupt Enable 1 mask
	#define EN_ALARM_INT1_bm	0x02
	//Alarm Interrupt Enable 1 bit position
	#define EN_ALARM_INT1_bp	1
	//Alarm Interrupt Enable 0 mask
	#define EN_ALARM_INT0_bm	0x01
	//Alarm Interrupt Enable 0 bit position
	#define EN_ALARM_INT0_bp	0
	//Enable all features in control register
	#define EN_ALL_CONTROL_gc	0x47

/**
 * Functions
 */
inline uint8_t RTCTransfer( uint8_t aReg, uint8_t aDat )
{
   uint8_t spiByte;

   // Clear SPIE bit if set
   spiByte = SPSR;
   spiByte = SPDR;

   // Set CS to high to enable DS1306
   SPI_DDR |= _BV(SS_bp);

   // Send register, then send/read data
   transferSPI( aReg );
   spiByte = transferSPI( aDat );

   // Set CS to low to disable DS1306
   SPI_DDR &= ~_BV(SS_bp);

   return spiByte;
}
inline void WriteRTC( uint8_t aReg, uint8_t aDat )
{
   RTCTransfer( aReg, aDat );
}

inline uint8_t ReadRTC( uint8_t aReg )
{
   return RTCTransfer( aReg, 0 );
}
inline uint8_t getSeconds()
{
	uint8_t ret = ReadRTC(SEC_R);
	uint8_t tens = ((ret & SEC10_MASK)>>4)*10;
	uint8_t ones = ret & SEC_MASK;
	return (tens+ones);
}
inline uint8_t getSecondsHex()
{
	return ReadRTC(SEC_R);
}

inline uint8_t getMinutes()
{
	uint8_t ret = ReadRTC(MIN_R);
	uint8_t tens = ((ret & MIN10_MASK)>>4)*10;
	uint8_t ones = ret & MIN_MASK;
	return(tens+ ones);
}
inline uint8_t getMinutesHex()
{
	return ReadRTC(MIN_R);
}

inline uint8_t getHours()
{
	uint8_t ret = ReadRTC(HOUR_R);
	uint8_t tens = ((ret & HOURS10_MASK)>>4)*10;
	uint8_t ones = ret & HOURS_MASK;
	return(tens+ ones);
}
inline uint8_t getHoursHex()
{
	uint8_t ret = ReadRTC(HOUR_R);
	return (ret & 0x1F);
}

inline uint8_t getAMPM()
{
	uint8_t ret = ReadRTC(HOUR_R);
	if((ret & AM_PM_MASK))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
void setTime(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t AMPM);


#endif /* DS1306_H_ */
